<?php
require_once 'ctlAbstract.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/generalFunctions.php';

/**
 * 
 * This class controls login functions
 * @author Minh-Tuan
 *
 */
class LogoutController extends AbstractController {
	/**
	 * This function will be called by the router
	 */
	public function main(array $getVars) {
		global $ubi;		
		
		//check to see if a user has logged in
		if (isset($_SESSION['userid'])) {
			//the user has logged in, log him out
			session_destroy();
			
			//if user checked remember me, delete those cookies
			if (isset($_COOKIE['userid']) || isset($_COOKIE['username']) || isset($_COOKIE['email'])) {
				setcookie('userid', '', time() - 3600);
				setcookie('username', '', time() - 3600);
				setcookie('email', '', time() - 3600);
				setcookie('roleid', '', time() - 3600);
				setcookie('rem', '', time() - 3600);
			}
			
			//delete session's variables
			$_SESSION[] = array();
			
			//if there is cookie of the session, delete it too
			/*if (isset($_COOKIE[session_name()])) {
				setcookie(session_name(), '', time() - 11);
			}*/
			
			$notice = "LogoutSuccessfully";
			$not_display_user = true;
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewNotice.php';
		} else {
			//the user hasn't logged in, redirect him to the home page
			header('Location:' . 'index.php');
		}
	}
}
?>